package com.eastcom.providor.dao.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.eastcom.bm.atom.Atom_AbnormalScanJob;
import com.eastcom.client.AbnormalScanJobBean;
import com.eastcom.providor.dao.BaseDao;
import com.eastcom.providor.dao.IScanJobDao;

public class ScanJobDao extends BaseDao implements IScanJobDao {

	public List<AbnormalScanJobBean> selectAllAbnormalScanJob()throws Exception {
		try{
			String sql="select s.*,d.name_cn from qa_abnormalscan_job s,qa_dataset_define d " +
					"where s.dataset_id=d.id";
			
			Map<Object,Map> results=getFieldMapWithKeyFildBySQL(sql, "dataset_id", new String[]{"first_time",
					"end_time","interval","backscan_hour","name_cn"});
			
			if(results==null||results.isEmpty())
				return null;
			
			List<AbnormalScanJobBean> abnormalScanJobBeans=new ArrayList<AbnormalScanJobBean>();
			for(Object id:results.keySet()){
				AbnormalScanJobBean abnormalScanJobBean=new AbnormalScanJobBean();
				
				Map infoMap=results.get(id);
				abnormalScanJobBean.setDataSetId((String)id);
				abnormalScanJobBean.setDataSetName((String)infoMap.get("name_cn"));
				abnormalScanJobBean.setFirstTime((Date)infoMap.get("first_time"));
				abnormalScanJobBean.setEndTime((Date)infoMap.get("end_time"));
				abnormalScanJobBean.setInterval(infoMap.get("interval")==null?0:((BigDecimal)infoMap.get("interval")).longValue());
				abnormalScanJobBean.setBackScanHour(infoMap.get("backscan_hour")==null?0:((BigDecimal)infoMap.get("backscan_hour")).intValue());
				abnormalScanJobBeans.add(abnormalScanJobBean);
			}
			
			return abnormalScanJobBeans;
		}
		catch(Exception e){
			throw e;
		}
	}
	
	public List<Atom_AbnormalScanJob> selectAllAtomAbnormalScanJob()throws Exception{
		try{
			return getObjects(Atom_AbnormalScanJob.class);
		}
		catch(Exception e){
			throw e;
		}
	}
	
	public void saveAbnormalScanJob(Atom_AbnormalScanJob abnormalScanJob)throws Exception{
		try{
			saveObject(abnormalScanJob);
		}
		catch(Exception e){
			throw e;
		}
	}

	public void removeAbnormalScanJob(String datasetId)throws Exception{
		try{
			this.deleteObjects("delete from Atom_AbnormalScanJob where dataSetId='"+datasetId+"'", null);
		}
		catch(Exception e){
			throw e;
		}
	}

	public Atom_AbnormalScanJob selectAbnormalScanJob(String datasetId) throws Exception {
		try {
			return this.getObject(Atom_AbnormalScanJob.class, datasetId);
		} catch (Exception e) {
			throw e;
		}
	}
	
	public void updateAbnormalScanJob(Atom_AbnormalScanJob scanJob) throws Exception{
		try {
			this.updateObject(scanJob);
		} catch (Exception e) {
			throw e;
		}
	}
}
